package org.simple.system.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.jwt.SaJwtUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.simple.dto.PageResult;
import org.simple.system.dto.auth.LoginParam;
import org.simple.system.dto.onlineuser.OnlineUserDto;
import org.simple.system.dto.onlineuser.OnlineUserQuery;
import org.simple.exception.CustomException;
import org.simple.system.service.IAuthService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 验证码控制器
 *
 * @author yh_liu
 * @version v1.0
 * @since 2022-11-24 22:15:00
 */
@RestController
@AllArgsConstructor
@RequestMapping("/center/system/auth")
@Tag(name = "auth", description = "登录相关")
public class AuthController {

    private final IAuthService authService;

    @SaIgnore
    @PostMapping("/doLogin")
    @Operation(summary = "账号密码登录")
    public String doLogin(@RequestBody @Valid LoginParam loginParam) throws CustomException {
        return authService.loginByUserName(loginParam);
    }

    @GetMapping("logout")
    @Operation(summary = "注销登录")
    public void logout() {
        StpUtil.logout(StpUtil.getLoginId(), StpUtil.getLoginDevice());
    }

    @GetMapping("userLogout")
    @Operation(summary = "管理员踢人下线")
    public void userLogout(String userId) {
        StpUtil.kickout(userId);
    }

    @GetMapping("list")
    @Operation(summary = "查询在线用户")
    public PageResult<OnlineUserDto> getList(OnlineUserQuery query) {
        List<OnlineUserDto> list = authService.getOnlineUser(query);
        PageResult<OnlineUserDto> pageResult = new PageResult<>(list);
        pageResult.setTotal(query.getTotal());
        return pageResult;
    }

    @GetMapping("check")
    @Operation(summary = "解析token")
    public void parseToken( ) throws IOException {
        // 这个是解析token
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySW5mbyI6eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiIxIiwicm5TdHIiOiJCWmMzVDJ4eUlYM0NmZ21WbUlrenZqSDNIWHlvVVhRVCIsInVzZXJuYW1lIjoic3lzdGVtIiwicGFzc3dvcmQiOiJhMzgzNjA2YjgxMjc1Mzc2ZDk3YzY0YmVlMzU4MWM2ZSIsIm5pY2tOYW1lIjoi6LaF57qn566h55CG5ZGYIiwicGhvbmUiOiIxNjYyODUxNTY3MiIsImVtYWlsIjoiZnJzaW1wbGVAMTYzLmNvbSIsInN0YXR1cyI6IjAiLCJhdmF0YXIiOiJodHRwczovL3BlbmdwZW5neXUtdGVzdC5vc3MtY24temhhbmdqaWFrb3UuYWxpeXVuY3MuY29tL3B1YmxpYy8yMDIyLTA2LTI2LzEv55S15a2Q5ZCN54mHLnBuZyIsImxvZ2luRGF0ZSI6MTY2ODE0OTg2NCwiZXJyb3JDb3VudCI6MCwibG9ja1RpbWUiOjE2Njg2Nzk1NjEsInRlbmFudCI6IjEiLCJvcmdhbiI6IjEiLCJwZXJtaXNzaW9ucyI6WyJzaG9wOm1haW46cXVlcnkiLCJzaG9wOm1haW46YWRkIiwic2hvcDptYWluOmVkaXQiLCJzaG9wOm1haW46ZGVsIiwic2hvcDptYWluOm9wZW5vcmNsb3NlIiwic2hvcDptZXNzYWdlOnF1ZXJ5Iiwic2hvcDptZXNzYWdlOmFkZCIsInNob3A6bWVzc2FnZTplZGl0Iiwic2hvcDptZXNzYWdlOmRlbCIsInNob3A6bWVzc2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N0ZW06b3JnYW46ZWRpdCIsInN5c3RlbTpvcmdhbjpkZWwiLCJzeXN0ZW06b3JnYW46cXVlcnkiLCJjZW50ZXI6bWVudTphZGQiLCJjZW50ZXI6bWVudTplZGl0IiwiY2VudGVyOm1lbnU6ZGVsIiwic3lzdGVtOnVzZXI6YWRkIiwic3lzdGVtOnVzZXI6ZWRpdCIsInN5c3RlbTp1c2VyOmRlbCIsInN5c3RlbTp1c2VyOnF1ZXJ5Iiwic3lzdGVtOnJvbGU6YWRkIiwic3lzdGVtOnJvbGU6cXVlcnkiLCJzeXN0ZW06cm9sZTplZGl0Iiwic3lzdGVtOnJvbGU6ZGVsIiwic3lzdGVtOmRpY3Q6YWRkIiwic3lzdGVtOmRpY3Q6ZWRpdCIsInN5c3RlbTpkaWN0OmRlbCIsInN5c3RlbTpkaWN0OnF1ZXJ5Iiwic3lzdGVtOm9ubGluZTpxdWVyeSIsInN5c3RlbTpvbmxpbmU6ZGVsIiwiY2VudGVyOm1lbnU6cXVlcnkiXSwiaWQiOiIxIn19.KDv1t53attc8ALqc3X8BgIZILLJFiIDPcWtbOm8EqG8";
        cn.hutool.jwt.JWT jwt = JWTUtil.parseToken(token);
        JSONObject payloads = jwt.getPayloads();

        // 验证签名
        Algorithm algorithm=Algorithm.HMAC256("ctac");
        JWTVerifier verifier=JWT.require(algorithm).build();

        DecodedJWT verify = verifier.verify(token);
        // 如果验证异常 会报错，没报错就验证通过
    }

    @SaIgnore
    @GetMapping("/getCode")
    @Operation(summary = "获取验证码")
    public void getCode(HttpServletResponse response, @RequestParam("sp") String sp) throws IOException {

        authService.getCode(response, sp);
    }
}
